package com.example.administrator.daydayappmarket.recommend.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendMainThirdActivity;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendTopImageSpecialThirdActivity;
import com.example.administrator.daydayappmarket.recommend.moduls.RecommendFootItem;
import com.example.administrator.daydayappmarket.recommend.moduls.RecommendMainItem;
import com.example.administrator.daydayappmarket.recommend.moduls.RecommendParentItem;
import com.example.administrator.daydayappmarket.recommend.moduls.RecommendPicItem;
import com.example.administrator.daydayappmarket.recommend.moduls.RecommendTopItem;
import com.example.administrator.daydayappmarket.recommend.view.MyGridView;
import com.example.administrator.daydayappmarket.recommend.view.MyListView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/12/26.
 * <p>
 * TODO 推荐主体的ListView的适配器
 */

public class RecommendMainAdapter extends BaseAdapter {

    private List<RecommendParentItem> data;
    private Context context;

    public RecommendMainAdapter(List<RecommendParentItem> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
//        data != null ? data.size() : 0
        return data != null ? data.size() : 0;
    }

    @Override
    public RecommendParentItem getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {

        //多布局
        String theme_type = data.get(position).getTheme_type();
        if (theme_type.equals("entry")) {
            return 0;
        } else if (theme_type.equals("app_topic")) {
            return 1;
        } else if (theme_type.equals("apps")) {
            return 2;
        } else if (theme_type.equals("pic_topic")) {
            return 3;
        }
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        ViewHolder1 viewHolder1 = null;
        ViewHolderfoot viewHolderfoot = null;
        ViewHolderpic viewHolderpic = null;

//        if(convertView == null){
//            switch (getItemViewType(position)) {
//                case 0:
//                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_listciew_top, parent, false);
//                    viewHolder = new ViewHolder(convertView);
//                    convertView.setTag(viewHolder);
//                    break;
//                case 1:
//                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_listview_main, parent, false);
//                    viewHolder1 = new ViewHolder1(convertView);
//                    convertView.setTag(viewHolder1);
//                    break;
//                case 2:
//                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_listview_foot, parent, false);
//                    viewHolderfoot = new ViewHolderfoot(convertView);
//                    convertView.setTag(viewHolderfoot);
//                    break;
//
//            }
//        }else{
//            switch (getItemViewType(position)) {
//                case 0:
//                    viewHolder = (ViewHolder) convertView.getTag();
//                    break;
//                case 1:
//                    viewHolder1 = (ViewHolder1) convertView.getTag();
//                    break;
//                case 2:
//                    viewHolderfoot = (ViewHolderfoot) convertView.getTag();
//                    break;
//            }
//        }
//
//        switch (getItemViewType(position)) {
//            case 0:
//                RecommendParentItem item = getItem(position);
//                RecommendTopItem topItem = (RecommendTopItem) item;
//                RecommendTopGridViewAdapter adapter = new RecommendTopGridViewAdapter(topItem.getData(), context);
//                viewHolder.topGridView.setAdapter(adapter);
//                return  convertView;
//            case 1:
//                RecommendParentItem item1 = getItem(position);
//                RecommendMainItem mainItem = (RecommendMainItem) item1;
////                if (mainItem.getTopic_name() != null && mainItem.getTopic_title() != null) {
//                viewHolder1.textTitle.setText(mainItem.getTopic_title());
//                viewHolder1.textName.setText(mainItem.getTopic_name());
////                }
//
//                RecommendMainGridViewAdapter adapter1 = new RecommendMainGridViewAdapter(mainItem.getData(), context);
//                viewHolder1.gridview.setAdapter(adapter1);
//                return convertView;
//            case 2:
//                RecommendMainFootListViewAdapter adapter2 = new RecommendMainFootListViewAdapter(((RecommendFootItem) getItem(position)).getData(), context);
//                viewHolderfoot.layoutRecommendMainListviewFootListView.setAdapter(adapter2);
//                return convertView;
//        }
        switch (getItemViewType(position)) {
            case 0:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_listciew_top, parent, false);
                    viewHolder = new ViewHolder(convertView);
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                RecommendParentItem item = getItem(position);
                RecommendTopItem topItem = (RecommendTopItem) item;
                RecommendTopGridViewAdapter adapter = new RecommendTopGridViewAdapter(topItem.getData(), context);
                viewHolder.topGridView.setAdapter(adapter);
                return convertView;

            case 1:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_listview_main, parent, false);
                    viewHolder1 = new ViewHolder1(convertView);
                    convertView.setTag(viewHolder1);
                } else {
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                }
                RecommendParentItem item1 = getItem(position);
                RecommendMainItem mainItem = (RecommendMainItem) item1;
//                if (mainItem.getTopic_name() != null && mainItem.getTopic_title() != null) {
                viewHolder1.textTitle.setText(mainItem.getTopic_title());
                viewHolder1.textName.setText(mainItem.getTopic_name());
//                }

                RecommendMainGridViewAdapter adapter1 = new RecommendMainGridViewAdapter(mainItem.getData(), context);
                viewHolder1.gridview.setAdapter(adapter1);

                return convertView;
            case 2:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.layout_recommend_main_listview_foot, parent, false);
                    viewHolderfoot = new ViewHolderfoot(convertView);
                    convertView.setTag(viewHolderfoot);
                } else {
                    viewHolderfoot = (ViewHolderfoot) convertView.getTag();
                }

                RecommendMainFootListViewAdapter adapter2 = new RecommendMainFootListViewAdapter(((RecommendFootItem) getItem(position)).getData(), context);
                viewHolderfoot.layoutRecommendMainListviewFootListView.setAdapter(adapter2);

                return convertView;

            case 3:
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend_main_listview_pic_item, parent, false);
                    viewHolderpic = new ViewHolderpic(convertView);
                    convertView.setTag(viewHolderpic);
                } else {

                    viewHolderpic = (ViewHolderpic) convertView.getTag();
                }
                RecommendParentItem item2 = getItem(position);
                final RecommendPicItem picItem = (RecommendPicItem) item2;
                viewHolderpic.picItemTitle.setText(picItem.getTopic_title());
                viewHolderpic.picItemName.setText(picItem.getTopic_name());
                Glide.with(context).load(Contacts.URL_BASEIMAGE+picItem.getImage()).into(viewHolderpic.picItemImage);

                viewHolderpic.picItemImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, RecommendTopImageSpecialThirdActivity.class);
                        intent.putExtra("id",picItem.getId());
                        context.startActivity(intent);
                    }
                });
                return convertView;
        }

        return convertView;
    }


    static class ViewHolder1 {
        @InjectView(R.id.layout_recommend_main_listview_main_text_title)
        TextView textTitle;
        @InjectView(R.id.layout_recommend_main_listview_main_text_name)
        TextView textName;
        @InjectView(R.id.layout_recommend_main_listview_main_gridview)
        MyGridView gridview;

        ViewHolder1(View view) {
            ButterKnife.inject(this, view);
        }
    }


    static class ViewHolderfoot {
        @InjectView(R.id.layout_recommend_main_listview_foot_listView)
        MyListView layoutRecommendMainListviewFootListView;

        ViewHolderfoot(View view) {
            ButterKnife.inject(this, view);
        }
    }



    static class ViewHolder {
        @InjectView(R.id.layout_recommend_main_listView_topgridView)
        MyGridView topGridView;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

    static class ViewHolderpic {
        @InjectView(R.id.layout_recommend_main_listview_main_pic_item_title)
        TextView picItemTitle;
        @InjectView(R.id.layout_recommend_main_listview_main_pic_item_name)
        TextView picItemName;
        @InjectView(R.id.item_recommend_main_listview_pic_item_image)
        ImageView picItemImage;

        ViewHolderpic(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
